package com.xinyuan.controller;

import com.xinyuan.entity.FileExist;
import com.xinyuan.utils.AmecoUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * @Classname FileExistController
 * @Description TODO
 * @Date 2020/6/9 8:47
 * @Created by zfl
 */
@RestController
public class FileExistController extends HttpServlet {

    @PostMapping("/fileExist")
    public void fileExist(@RequestParam("id") String fileID, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.addHeader("Access-Control-Allow-Origin","*");
        String filePathCamp = "C:\\upload\\" + fileID + "\\CAMP.xlsx";
        String filePathMtop = "C:\\upload\\" + fileID + "\\MTOPONE.xlsx";
        String filePathTime = "C:\\upload\\" + fileID + "\\TIME.xls";
        File fileCamp = new File(filePathCamp);
        File fileMtop = new File(filePathMtop);
        File fileTime = new File(filePathTime);
        AmecoUtils utils = new AmecoUtils();
        if(fileCamp.exists() && fileMtop.exists() && fileTime.exists()) {
            FileExist fileExist = new FileExist();
            fileExist.setErrCode(0);
            fileExist.setMsg("文件全部上传成功！");
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(utils.getJson(fileExist));
        }else {
            FileExist fileExist = new FileExist();
            fileExist.setErrCode(1);
            fileExist.setMsg("缺少文件！");
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(utils.getJson(fileExist));
        }
    }
}
